#include <stdio.h>
#include "CuDebug.h"

__device__ CuDebugData::CuDebugData()
{
	ref = 0;
}

__device__ CuDebugData::~CuDebugData()
{
	assert(ref == 0);
	for(int i = 0; i < strs.size(); i++)
	{
		printf("%s ",strs[i].c_str());
	}
	printf("\n");
}

//CuDebug
__device__ CuDebug::CuDebug()
{	
	data = new CuDebugData();
	data->ref = 1;
}

__device__ CuDebug::CuDebug(const CuDebug& other)
{
	data = other.data;
	data->ref++;
}

__device__ CuDebug::~CuDebug()
{	
	data->ref--;
	if(data->ref == 0)
		delete data;
}

    
__device__ void CuDebug::operator=(const CuDebug& other)
{
	data = other.data;
	data->ref++;
}

__device__ CuDebug cuDebug()
{
	CuDebug dbg;
	return dbg;
}

__device__ CuDebug operator<<(CuDebug dbg, const char* str)
{
	dbg.data->strs.push_back(str);
	return dbg;
}

__device__ CuDebug operator<<(CuDebug dbg, int num)
{
	dbg.data->strs.push_back(CuString::number(num));
	return dbg;
}

__device__ CuDebug operator<<(CuDebug dbg, uint32_t num)
{
	dbg.data->strs.push_back(CuString::number(num));
	return dbg;
}

__device__ CuDebug operator<<(CuDebug dbg, int64_t num)
{
	dbg.data->strs.push_back(CuString::number(num));
	return dbg;
}

__device__ CuDebug operator<<(CuDebug dbg, uint64_t num)
{
	dbg.data->strs.push_back(CuString::number(num));
	return dbg;
}

__device__ CuDebug operator<<(CuDebug dbg, const CuString &str)
{
	dbg.data->strs.push_back(str);
	return dbg;
}